<?php
namespace app\index\controller;
use app\service\apiService;
use app\service\cxyunService;
use app\service\invoiceService;
use app\service\orderService;
use think\facade\View;
use think\request;
use think\facade\Db;
use think\facade\Log;
error_reporting(0);
class Orderali extends Alipay{
    public function carquery(){
        $user_id=session("user_id");
        View::assign("user_id",session("user_id"));
        $parking_id=request()->get("id");
        $parkinglotinfo=Db::table("parkinglotinfo")->where("id",$parking_id)->find();
        if(empty($parkinglotinfo)){
            return "停车场信息错误";
        }
        View::assign("id",$parking_id);
        return View::fetch();
    }
    public function prepay(){
        $parking_id=request()->get("id");
	    $carPlateNo=request()->get("car_number");
        $parkingInfo=Db::table("parkinglotinfo")->where("id",$parking_id)->find();
	    if(empty($parkingInfo)){
	        return "停车场信息错误";
	    }

        $data["parkingId"]=$parkingInfo["cxyun_parking_id"];
        $data["plateNumber"]=$carPlateNo;
        $data["timestamp"]=time()."000";
        $notData=array();
        $cxyunApi=new cxyunService($parkingInfo["cxyun_appkey"]);
        $res=$cxyunApi->commonPost("calculatingTempCost",$data,$notData);
        $result= json_decode($res,true);

        if($result["status"]==0){
            if($result["message"]=="获取临停缴费接口,请检查车场网络!!"){
                return View::fetch("offline");
            }else if($result["message"]=="找不到入场记录!"){
                return View::fetch("nomsg");
            }else if($result["message"]=="不需要缴费!"){
                return View::fetch("nomsg");
            }
            //return json(["code"=>500,"msg"=>$result["message"]]);
            return $result["message"];

        }
        $result= json_decode($result["Data"],true);
        $add["parkingId"]=$result["parkingId"];
        $add["parkingNo"]=$parkingInfo["parkingNo"];
        $add["parkingName"]=$parkingInfo["parkingName"];
        $add["plateNumber"]= $data["plateNumber"];
        $add["payFee"]=$result["payFee"];
        $add["totalFee"]=$result["totalFee"];
        $add["upayFee"]=$result["upayFee"];
        $add["orderNo"]=$result["orderNo"];
        $add["stopTime"]=$result["stopTime"];
        $add["inTime"]=$result["inTime"];//$orderinfo.parkingStartTime|strtotime|date="Y-m-d H:i:s"
        $add["discountFee"]=$result["discountFee"];
        $add["user_id"]=session("user_id");;
        $add["createTime"]=date("Y-m-d H:i:s");
        //$add["recordId"]=$car_out["recordId"];
//        $add["cartypeName"]=$car_out["cartypeName"];
//        $add["carmodelName"]=$car_out["carmodelName"];
//        $add["inLaneCode"]=$car_out["inLaneCode"];
//        $add["outLaneCode"]=$car_out["outLaneCode"];
        $add["id"]= Db::table("cxyun_query_order_log")->insertGetId($add);
        View::assign("duration",convertTime($add["stopTime"]*60));
        $add["inTime"]=substr($add["inTime"], 0, -4);
        View::assign("order_info",$add);
        View::assign("parkinginfo",$parkingInfo);

        return View::fetch();

    }
    public function prepayOrder(){
        $user_id=session("user_id");
		$pay_id=request()->post("pay_id");
		$payinfo=Db::table("cxyun_query_order_log")->where("id",$pay_id)->find();
		if(empty($payinfo)){
		    return json(['info' => "查询账单失败"]);
		}

        $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$payinfo["parkingNo"])->find();
        if(empty($parkinginfo)){
            return json(['info' => "停车场信息错误"]);
        }
        $outTradeNo=date("YmdHis").rand(100000,999999);

        $token = $parkinginfo["accredit"];
        $subject = $payinfo["parkingName"]."-".$payinfo["plateNumber"];
        $notifyUrl=$this->getCurrentUrl()."/index/callback/alinotice";
        $api = new apiService();
        $data=array(
            'out_trade_no'=>$outTradeNo,
            'total_amount'=>$payinfo["upayFee"],
            'buyer_id'=>$user_id,
            'subject' =>$subject,
            'token'=>$token ,
            'notifyurl'=>$notifyUrl,
        );

        $result = $api->tradeCreate(json_encode($data));
        if(property_exists($result,'sub_msg')){
            return json(['info' => $result->sub_msg]);
        }else{
            unset($payinfo["upayFee"]);
            unset($payinfo["id"]);
            $info=$payinfo;
            $info['query_order_log_id']=$pay_id;
            $info['tradeNo'] =  $result->trade_no;
            $info["subject"]=$subject;
            $info['outTradeNo'] =$outTradeNo;
            $info["token"]=$token;
            $info["scenes"]=2;//支付宝预支付
            $info['createTime'] =  date("y-m-d H:i:s");
            $res=Db::table("cxyun_order")->insert($info);
            //返回结果到前台唤起收银台
            if($res){
                return json(['order_no' => $info['tradeNo']]);
            }else{
                return json(['info' => '订单存储失败']);
            }
        }

    }
    public function outpay(){
        $user_id=session("user_id");
        $id=request()->get("id");
        //查询出口信息
	    $gateinfo=Db::table("cxyun_parking_gate")->where("id",$id)->find();
	    if(empty($gateinfo)){
	        return "出口信息不存在";
	    }
	    View::assign("gateinfo",$gateinfo);


        $parkingInfo=Db::table("parkinglotinfo")->where("parkingNo",$gateinfo["parkingNo"])->find();
        View::assign("parkingInfo",$parkingInfo);
        $car_out=Db::table("cxyun_car_out")->where("outLaneCode",$gateinfo['gateId'])->find();
        if(empty($car_out)){
            return View::fetch("nomsg");
            return "无车辆出场信息";
        }
        $data["parkingId"]=$parkingInfo["cxyun_parking_id"];
        $data["plateNumber"]=$car_out["plateNumber"];
        $data["timestamp"]=time()."000";
        $notData=array();
        $cxyunApi=new cxyunService($parkingInfo["cxyun_appkey"]);
        $res=$cxyunApi->commonPost("calculatingTempCost",$data,$notData);
        $result= json_decode($res,true);
        if($result["status"]==0){
            if($result["message"]=="获取临停缴费接口,请检查车场网络!!"){
                return View::fetch("offline");
            }else if($result["message"]=="找不到入场记录!"){
                return View::fetch("nomsg");
            }else if($result["message"]=="不需要缴费!"){
                return View::fetch("nomsg");
            }
            return $result["message"];
            //return json(["code"=>500,"msg"=>$result["message"]]);
        }
        $result= json_decode($result["Data"],true);

        $add["parkingId"]=$result["parkingId"];
        $add["parkingNo"]=$parkingInfo["parkingNo"];
        $add["parkingName"]=$parkingInfo["parkingName"];
        $add["plateNumber"]= $data["plateNumber"];
        $add["payFee"]=$result["payFee"];
        $add["totalFee"]=$result["totalFee"];
        $add["upayFee"]=$result["upayFee"];
        $add["orderNo"]=$result["orderNo"];
        $add["stopTime"]=$result["stopTime"];
        $add["inTime"]=$result["inTime"];
        $add["discountFee"]=$result["discountFee"];
        $add["user_id"]=$user_id;;
        $add["createTime"]=date("Y-m-d H:i:s");
        $add["recordId"]=$car_out["recordId"];
        $add["cartypeName"]=$car_out["cartypeName"];
        $add["carmodelName"]=$car_out["carmodelName"];
        $add["inLaneCode"]=$car_out["inLaneCode"];
        $add["outLaneCode"]=$car_out["outLaneCode"];
        $add["id"]= Db::table("cxyun_query_order_log")->insertGetId($add);
        View::assign("duration",convertTime($add["stopTime"]*60));
        $add["inTime"]=substr($add["inTime"], 0, -4);
        View::assign("order_info",$add);
        $gateinfo=Db::table("cxyun_parking_gate")->where("gateId",$add["outLaneCode"])->find();
        View::assign("gateinfo",$gateinfo);

        return View::fetch();
    }
    
    public function createOrder(){
        $user_id=session("user_id");
		$id=request()->post("id");
		$payinfo=Db::table("cxyun_query_order_log")->where("id",$id)->find();
		if(empty($payinfo)){
		    return json(['info' => "查询账单失败"]);
		}
        $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$payinfo["parkingNo"])->find();
        if(empty($parkinginfo)){
            return json(['info' => "停车场信息错误"]);
        }
        $outTradeNo=date("YmdHis").rand(100000,999999);

		$token = $parkinginfo["accredit"];
        $subject = $payinfo["parkingName"]."-".$payinfo["plateNumber"];
        $notifyUrl=$this->getCurrentUrl()."/index/callback/alinotice";
        $api = new apiService();
        $data=array(
            'out_trade_no'=>$outTradeNo,
            'total_amount'=>$payinfo["upayFee"],
            'buyer_id'=>$user_id,
            'subject' =>$subject,
            'token'=>$token ,
            'notifyurl'=>$notifyUrl,
        );

        $result = $api->tradeCreate(json_encode($data));
		if(property_exists($result,'sub_msg')){	
			return json(['info' => $result->sub_msg]);
		}else{
            unset($payinfo["upayFee"]);
		    unset($payinfo["id"]);
		    $info=$payinfo;
            $info['query_order_log_id']=$id;
			$info['tradeNo'] =  $result->trade_no;
			$info["subject"]=$subject;
            $info['outTradeNo'] =$outTradeNo;
            $info["token"]=$token;
            $info["scenes"]=1;
            $info['createTime'] =  date("y-m-d H:i:s");
			$res=Db::table("cxyun_order")->insert($info);
			//返回结果到前台唤起收银台
			if($res){
				return json(['order_no' => $info['tradeNo']]);
			}else{
				return json(['info' => '订单存储失败']);
			}
		}	
	}
}